<?php

namespace app\admin\model\sales;

use think\Model;

/**
 * Debtortrans
 */
class Debtortrans extends Model
{
    // 表名
    protected $name = 'sales_debtortrans';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 追加属性
    protected $append = [
        'typeTable',
    ];


    public function getTypeTableAttr($value, $row): array
    {
        return [
            'typename' => \app\admin\model\setup\Systypes::whereIn('typeid', $row['type'])->column('typename'),
        ];
    }

    public function getTypeAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setTypeAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function debtornoTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\sales\Debtorsmaster::class, 'debtorno', 'debtorno');
    }

    public function branchcodeTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\sales\Custbranch::class, 'branchcode', 'branchcode');
    }

    public function prdTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\fm\Periods::class, 'prd', 'periodno');
    }

    public function tpeTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\sales\Salestypes::class, 'tpe', 'typeabbrev');
    }

    public function salespersonTable(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\sales\Salesman::class, 'salesperson', 'salesmancode');
    }
}